import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from "@/views/Index.vue"
import { get } from '@/utils/local'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
return originalPush.call(this, location).catch(err => err)
}

const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location, onResolve, onReject) {
if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
return originalReplace.call(this, location).catch(err => err)
}



const routes = [
  {
   path:'/',
   redirect:'/index'
  },
  {
    name:'det',
    path:'/det',
    component:()=>import("@/views/Detail.vue")
  },
  {
    name:'login',
    path:'/login',
    component:()=>import("@/views/Login.vue")
  },
  {
    name:'addrlist',
    path:'/addrlist',
    component:()=>import("@/views/AddrList.vue")
  },
  {
    name:'address',
    path:'/address',
    component:()=>import("@/views/Address.vue")
  },
  {
    name:'order',
    path:'/order',
    component:()=>import("@/views/order.vue")
  },
   {
    name:'index',
    path:'/index',
    component:Index,
    children:[
      {
         path:'/index',
         redirect:'/index/home'
      },
      {
        name:'home',
        path:'home',
        component:()=>import("@/views/menu/Home.vue")
      },
      {
        name:'cate',
        path:'cate',
        component:()=>import("@/views/menu/Cate.vue")
      },
      {
        name:'car',
        path:'car',
        component:()=>import("@/views/menu/Car.vue")
      },
      {
        name:'my',
        path:'my',
        component:()=>import("@/views/menu/My.vue")
      },
    ]
   }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  const blackArr=['/index/car','order']

  // if(to.path=='/login'||to.path=='/index'||to.path=='det'||to.path=='/index/home'||to.path=='/index/cate'||to.path=='/index/my'){// 直接放行
  if(blackArr.includes(to.path)){
        // 判断是否登录  回话存储是否有用户信息
        let userinfo=get("userinfo")
        // console.log(userinfo); // 没有是 null 
        if(userinfo){// 已经登录
           next()
        }else{// 未登录
           // 去登录页面
           next("/login")
           // alert("请先登录")
        }
  }else{
  
     next()
     
  }
  
})
export default router
